package com.clstu.tankgame5;

import java.awt.*;
import java.util.Vector;

public class EnemyTank extends Tank implements Runnable{

    private static Vector<EnemyTank> enemyTanks = null;

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
       EnemyTank.enemyTanks = enemyTanks;
    }

    public EnemyTank(int x, int y,int direct) {
        super(x, y,direct);
        this.setSpeed(1);
    }

    private void sleepPlus(){
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private boolean isPointTouch(int x1,int y1,int x2,int y2,Tank e){
        if(e.getDirect()==0||e.getDirect()==2){//敌人坦克竖着
            if(x1<e.getX()+40 && x1>e.getX() && y1>e.getY() && y1<e.getY()+60
            || x2<e.getX()+40 && x2>e.getX() && y2>e.getY() && y2<e.getY()+60){
                return true;
            }
        }else {//敌人坦克横着
            if(x1<e.getX()+60 && x1>e.getX() && y1>e.getY() && y1<e.getY()+40
            || x2<e.getX()+60 && x2>e.getX() && y2>e.getY() && y2<e.getY()+40){
                return true;
            }
        }
        return false;
    }

    private boolean isTouchEnemyTank(){//判断是否发生重叠
        if(enemyTanks == null) return false;
        if(enemyTanks.size()<=1) return false;
        for (int i=0;i<enemyTanks.size();i++) {
            Tank e = (Tank) enemyTanks.get(i);
            if(e!=this){
                switch (getDirect()){//这里是要判断自己坦克的方向
                    case 0:
                        if( isPointTouch(getX(),getY(),getX()+40,getY(),e)) return true;
                        break;
                    case 1:
                        if( isPointTouch(getX(),getY(),getX(),getY()+40,e)) return true;
                        break;
                    case 2:
                        if( isPointTouch(getX(),getY()+60,getX()+40,getY()+60,e)) return true;
                        break;
                    case 3:
                       if( isPointTouch(getX()+60,getY(),getX()+60,getY()+40,e)) return true;
                       break;
                    default:
                       break;
                }
            }
        }
        return false;//如果全部都没有碰撞就返回false
    }

    @Override
    public void run() {
        while (isLive()){//让敌人的坦克随机的动起来,没有重叠就可以动，有重叠就不能动
            switch (super.getDirect()){
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if(!isTouchEnemyTank()) moveUP();
                        sleepPlus();
                    }
                    break;
                case 1:
                    for (int i = 0; i <30; i++) {
                        if(!isTouchEnemyTank()) moveLeft();
                        sleepPlus();
                    }
                    break;
                case 2:
                    for (int i = 0; i <30; i++) {
                        if(!isTouchEnemyTank()) moveDown();
                        sleepPlus();
                    }
                    break;
                case 3:
                    for (int i = 0; i <30; i++) {
                        if(!isTouchEnemyTank()) moveRight();
                        sleepPlus();
                    }
                    break;
                default:
                    break;
            }
            sleepPlus();
            setDirect((int)(Math.random()*4));//改变方向
            //如果发射的子弹已经消亡，就重新发射一颗子弹
            if(getShots().size()<=5){//修改为敌方最多同时发射5颗子弹
                Shot shot=shot(Color.yellow);
                new Thread(shot).start();
                getShots().add(shot);
            }
        }
    }
}
